﻿using ConfExpert.Forms.Base;
using DevExpress.XtraPivotGrid;
using DevTeam;

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Tela
{
    public partial class StokOzet : XForm
    {
        public StokOzet()
        {
            InitializeComponent();
        }

        BindingSource bs;
        private void StokOzet_Load(object sender, EventArgs e)
        {
            
            ds.Tela.Columns.Add("Qty", typeof(decimal));
            bs = new BindingSource(ds, "Tela");

            DrillDownGrid(gv, bs);
            gv.MakeColumnInvisible("ModifiedOn", "Deleted", "Art", "Col", "ColDesc", "isUsed");
            gv.FormatAsNumber(1, "Qty");
            gv.AddSummary("Qty");
            gv.ClearColumnEdit("ProductId");

            InitPivot(pvt, bs);
            pvt.CustomAppearance += new PivotCustomAppearanceEventHandler(pvt_CustomAppearance);
            pvt.FocusedCellChanged += new EventHandler(pvt_FocusedCellChanged);
            pvt.CellClick += new PivotCellEventHandler(pvt_CellClick);

            splitContainerControl2.AutoSize();
            ExpandCollapse(splitContainerControl2, AcKapa.Collapse);

            bbiGuncelle.ItemClick += bbiGuncelle_ItemClick;
            Rfrsh();
        }

        void bbiGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Rfrsh();
        }

        void Rfrsh()
        {
            gv.EndEditing();
            ds.Tela.Clear();
            GetDetails("Tela", "GetTelaStokOzet");

            gv.BestFitColumns();
            pvt.BestFit();
        }

        void pvt_CellClick(object sender, PivotCellEventArgs e)
        {
            CellChanged();
        }

        void pvt_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);

        }

        void pvt_FocusedCellChanged(object sender, EventArgs e)
        {
            CellChanged();
        }

        bool changing = false;
        private void CellChanged()
        {
            if (!changing)
                try
                {
                    changing = true;
                    if (!gc.IsDisposed && pvt.Cells.GetFocusedCellInfo() != null)
                        gc.DataSource = pvt.Cells.GetFocusedCellInfo().CreateDrillDownDataSource();
                    else
                        gc.DataSource = null;

                    pvt.Invalidate();
                    gv.BestFitColumns();
                    ExpandCollapse(splitContainerControl2, (gv.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));

                }
                finally
                {
                    changing = false;
                }
        }




        private void InitPivot(PivotGridControl pgc, BindingSource bs)
        {
            pgc.DataSource = bs;
            pgc.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
            pgc.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
            pgc.BeginUpdate();

            PivotGridFieldBase[] pgf2 = new PivotGridFieldBase[2];
            pgf2[0] = AddNewField(pgc, "TelaVendorId", "Üretici", PivotArea.RowArea);
            ((PivotGridField)pgf2[0]).FieldEdit = RI(Rp.TelaVendor);

            pgf2[1] = AddNewField(pgc, "Art", "Art", PivotArea.RowArea);

            //pgf2[0].Caption = "Durum";
            //((PivotGridField)pgf2[0]).FieldEdit = riIDLE;
            //pgf2[2].ExpandedInFieldsGroup = false;
            pgc.Groups.Add(pgf2, "Artikel");

            AddNewField(pgc, "Aciklama", "Aciklama", PivotArea.ColumnArea);

            AddNewField(pgc, "Agirlik", PivotArea.FilterArea);
            AddNewField(pgc, "TelaEn", PivotArea.FilterArea);
            AddNewField(pgc, "TelaCinsId", PivotArea.FilterArea).FieldEdit = RI(Rp.TelaCins);
            AddNewField(pgc, "TelaVendorId", PivotArea.FilterArea).FieldEdit = RI(Rp.TelaVendor);
            AddNewField(pgc, "TelaFinishId", PivotArea.FilterArea).FieldEdit = RI(Rp.TelaFinish);

            AddNewField(pgc, "Qty", "Metraj", PivotArea.DataArea);

            //.Width = 40;  .Visible = true;
            //    fld.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Sum;

            foreach (PivotGridField pgf in pgc.Fields)
            {
                pgf.SortOrder = PivotSortOrder.Ascending;
                pgf.Options.AllowSort = DevExpress.Utils.DefaultBoolean.True;
            }
            pgc.EndUpdate();
        }

        private PivotGridField AddNewField(PivotGridControl pgc, string FieldName, string Caption, PivotArea pa)
        {
            PivotGridField fd = new PivotGridField();
            fd.FieldName = FieldName;
            fd.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            fd.CellFormat.FormatString = "#,###.0";
            fd.SetAreaPosition(pa, 0);
            pgc.Fields.Add(fd);
            fd.Name = "_" + FieldName;
            fd.Caption = Caption;
            return fd;
        }


    }
}
